package org.example.petitionplatformsystem.controller;

import org.example.petitionplatformsystem.dao.model.Employees;
import org.example.petitionplatformsystem.service.EmployeesService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/admin_panel/institution/{institutionid}/department/{departmentid}")
public class EmployeesController {

    //职员表的服务
    private final EmployeesService employeesService;
    @Autowired
    public EmployeesController(EmployeesService employeesService){
        this.employeesService = employeesService;
    }

    @PostMapping("/worker")
    public ResponseEntity<Map<String, Object>> addEmployee(@PathVariable Long institutionid, @PathVariable Long departmentid, @RequestParam String rank) {
        Employees employee = new Employees();
        employee.setInstitutionID(institutionid);
        employee.setDepartmentID(departmentid);
        employee.setRanks(rank);
        employeesService.addEmployee(employee);

        Map<String, Object> response = new HashMap<>();
        response.put("code", 0);
        response.put("desc", "Employee added successfully");


        return ResponseEntity.ok(response);
    }

    @PutMapping("/worker/{workid}")
    public ResponseEntity<Map<String, Object>> updateEmployee(@PathVariable Long institutionid, @PathVariable Long departmentid, @PathVariable Long workid, @RequestParam String ranks) {
        Employees employee = employeesService.getEmployeeById(workid);
        if (employee == null) {
            Map<String, Object> response = new HashMap<>();
            response.put("code", 1);
            response.put("desc", "Employee not found");
            return ResponseEntity.status(404).body(response);
        }
        employee.setRanks(ranks);
        employeesService.updateEmployee(employee);

        Map<String, Object> response = new HashMap<>();
        response.put("code", 0);
        response.put("desc", "Employee updated successfully");
        return ResponseEntity.ok(response);
    }

    @DeleteMapping("/worker/{workid}")
    public ResponseEntity<Map<String, Object>> deleteEmployee(@PathVariable Long institutionid, @PathVariable Long departmentid, @PathVariable Long workid) {
        employeesService.deleteEmployee(workid);

        Map<String, Object> response = new HashMap<>();
        response.put("code", 0);
        response.put("desc", "Employee deleted successfully");
        return ResponseEntity.ok(response);
    }

    @GetMapping("/worker/{workid}")
    public ResponseEntity<Map<String, Object>> getEmployeeById(@PathVariable Long institutionid, @PathVariable Long departmentid, @PathVariable Long workid) {
        Employees employee = employeesService.getEmployeeById(workid);
        if (employee == null) {
            Map<String, Object> response = new HashMap<>();
            response.put("code", 1);
            response.put("desc", "Employee not found");
            return ResponseEntity.status(404).body(response);
        }

        Map<String, Object> response = new HashMap<>();
        response.put("code", 0);
        response.put("desc", "Success");
        Map<String, Object> data = new HashMap<>();
        data.put("institutionid", employee.getInstitutionID());
        data.put("departmentid", employee.getDepartmentID());
        data.put("rank", employee.getRanks());
        response.put("data", data);
        return ResponseEntity.ok(response);
    }

    @GetMapping("/workers")
    public ResponseEntity<Map<String, Object>> getAllEmployees(@PathVariable Long institutionid, @PathVariable Long departmentid) {
        List<Employees> employeesList = employeesService.getAllEmployees();

        Map<String, Object> response = new HashMap<>();
        response.put("code", 0);
        response.put("desc", "Success");
        response.put("data", employeesList.stream().map(employee -> {
            Map<String, Object> data = new HashMap<>();
            data.put("institutionid", employee.getInstitutionID());
            data.put("departmentid", employee.getDepartmentID());
            data.put("rank", employee.getRanks());
            return data;
        }));
        return ResponseEntity.ok(response);
    }
}
